package com.ht.ui;

import com.ht.aems.swing.util.ListUtil;
import devmodel.MapObject;
import org.javabuilders.swing.SwingJavaBuilder;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: Yanxue
 * Date: 2011-9-1
 */
public class DataImportFromCimDialog extends JDialog {
    JList list;
    JPanel contentPanel;
    MapObject selectedObject;
    DefaultListModel listModel;

    public DataImportFromCimDialog() {
        list = new JList();
        listModel = new DefaultListModel();
        list.setModel(listModel);
        contentPanel = ListUtil.getSearchBarPanel(list, BorderLayout.NORTH);
        SwingJavaBuilder.build(this);
        setLocationRelativeTo(null);
    }

    public DataImportFromCimDialog(List<MapObject> plants) {
        this();
        for (MapObject obj : plants)
            listModel.addElement(obj);
    }

    public void okAction() {
        this.selectedObject = (MapObject) list.getSelectedValue();
        this.dispose();
    }

    public MapObject getSelectedObject() {
        return selectedObject;
    }

    public void cancelAction() {
        this.dispose();
    }

    public static void main(String[] args) {
        DataImportFromCimDialog cimImportDialog = new DataImportFromCimDialog();
        cimImportDialog.setVisible(true);
    }

    public void setListDevices(List<MapObject> devices) {
        this.selectedObject = null;
        listModel.removeAllElements();
        for (MapObject obj : devices)
            listModel.addElement(obj);
    }
}